package com.haha.gulimall.coupon.service.impl;

import com.haha.common.to.MemberPrice;
import com.haha.common.to.SkuReductionTo;
import com.haha.gulimall.coupon.dao.MemberPriceDao;
import com.haha.gulimall.coupon.entity.MemberPriceEntity;
import com.haha.gulimall.coupon.entity.SkuLadderEntity;
import com.haha.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.common.utils.PageUtils;
import com.haha.common.utils.Query;

import com.haha.gulimall.coupon.dao.SkuFullReductionDao;
import com.haha.gulimall.coupon.entity.SkuFullReductionEntity;
import com.haha.gulimall.coupon.service.SkuFullReductionService;

import javax.annotation.Resource;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    SkuLadderService skuLadderService;
    @Resource
    MemberPriceServiceImpl memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveReduction(SkuReductionTo skuReductionTo) {
        SkuLadderEntity skuLadder = new SkuLadderEntity();
        BeanUtils.copyProperties(skuReductionTo,skuLadder);
        skuLadder.setAddOther(skuReductionTo.getCountStatus());
        if(skuLadder.getFullCount()>0){
            skuLadderService.save(skuLadder);  //打折
        }


        SkuFullReductionEntity skuFullReduction = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,skuFullReduction);
        if(skuFullReduction.getFullPrice().signum()==1){
            this.save(skuFullReduction);   //满减
        }


        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(memberPrice1 -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(memberPrice1.getId());
            memberPriceEntity.setMemberLevelName(memberPrice1.getName());
            memberPriceEntity.setMemberPrice(memberPrice1.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item->{
            return item.getMemberPrice().signum()==1;
        }).collect(Collectors.toList());

        memberPriceService.saveBatch(collect); //会员价格

    }

}